package com.bear.sgg.springboot_06_web.config;

import com.bear.sgg.springboot_06_web.ThreeComponent.MyFilter;
import com.bear.sgg.springboot_06_web.ThreeComponent.MyListener;
import com.bear.sgg.springboot_06_web.ThreeComponent.MyServlet;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.imageio.spi.RegisterableService;

/**
 * @ClassNmae MyServerConfig
 * @Description TODO
 * @Author bear
 * @Date 2020/3/12 0012 下午 3:17
 */
@Configuration
public class MyServerConfig {

    /*
    * 注册web server三大组件：servlet、filter、listener
    * */


    /**
     * 配置servlet（用于处理请求）
     * @return
     */
    @Bean
    public ServletRegistrationBean<MyServlet> servletRegistrationBean() {
        return new ServletRegistrationBean<>(new MyServlet(), "/servlet");
    }

    /**
     * 配置Filter（用于过滤请求，相当于spring mvc的interceptor）
     * @return
     */
    @Bean
    public FilterRegistrationBean<MyFilter> filterFilterRegistrationBean() {
        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyFilter());
        registrationBean.addUrlPatterns("/servlet");
        return registrationBean;
    }

    /**
     * 配置listener（用于监听web应用的开始和结束）
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean<MyListener> listenerServletListenerRegistrationBean() {
        return new ServletListenerRegistrationBean<>(new MyListener());
    }



    /**
     * 配置嵌入Servlet容器，也可以在.properties文件中直接配置
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryWebServerFactoryCustomizer() {

        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8080);
            }
        };
    }

}
